package com.tarena.lbs.activity.controller;

import com.tarena.lbs.activity.dao.mapper.ActivityMapper;
import com.tarena.lbs.activity.feign.AdminClient;
import com.tarena.lbs.activity.pojo.param.ActivityUpdateStatusParam;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.activity.pojo.param.ActivityAddParam;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.sql.Wrapper;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 活动相关功能接口
 */
@Slf4j
@Api(tags = "活动相关功能接口")
@RestController
@RequiredArgsConstructor
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private AdminClient adminClient;
    @Autowired
    private JwtEncoder jwtEncoder;


    @ApiOperation("商家活动列表")
    @PostMapping("/admin/marketing/marketingActivity/info/list")

    public Result<PageResult<ActivityVO>> pageList( ServletRequest request)
            throws BusinessException {
        //获取全量数据
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String jwt = httpServletRequest.getHeader("Authorization");
        UserPrinciple userPrinciple =(UserPrinciple) jwtEncoder.getLoginFromToken(jwt,UserPrinciple.class);
        log.debug("当前登录用户信息:userPrinciple:{}",userPrinciple);
        Integer id = userPrinciple.getId();
        log.debug("当前登录用户的id id：{}",id);

        Result<AdminVO> result = adminClient.detail(id);
        log.debug("返回值 result:{}",result);
        AdminVO adminVO = result.getData();
        log.debug("用户信息 adminVO:{}",adminVO);
        Integer businessId= adminVO.getBusinessId();
        List<ActivityVO> list = activityService.list(Long.valueOf(businessId));
        //Long businessId=1l;
        //List<ActivityVO> list =activityService.list(businessId);
        //2.使用分页参数创建分页结果
        PageResult<ActivityVO> pageResult = new PageResult<>();
        pageResult.setObjects(list);
        pageResult.setPageNo(1);
        pageResult.setPageSize(10);
        pageResult.setTotal((long) list.size());
        log.debug("pageResult:{}",pageResult);
        return new Result<>(pageResult) ;
    }

    @PostMapping("/admin/marketing/marketingActivity/info/updateStatus")
    public void updateStatus(@RequestBody ActivityUpdateStatusParam updateStatusParam) throws BusinessException {
        log.debug("修改活动状态参数 updateStatusParam:{}",updateStatusParam);
        ActivityPO activityPO = new ActivityPO();
        Long activityId = updateStatusParam.getId();
        activityPO.setId(Math.toIntExact(activityId));
        activityPO.setStatus(updateStatusParam.getStatus());
        activityPO.setUpdateAt(new Date());
        log.debug("activityPO:{}",activityPO);
        activityMapper.updateById(activityPO);
    }

    @ApiOperation("商家活动保存")
    @PostMapping("/admin/marketing/marketingActivity/info/save")
    public Result<Void> activitySave(@RequestBody ActivityAddParam param)
            throws BusinessException, ParseException {
        log.debug("新增活动列表");
        activityService.insert(param);
        return Result.success();
    }

    @ApiOperation("商家活动详情")
    @GetMapping("/admin/marketing/marketingActivity/info/detail/{id}")
    public Result<ActivityVO> activityDetail(@PathVariable("id") Long id)
            throws BusinessException {
        log.debug("活动详情参数:{}", id);

        return activityService.detail(id);
    }
    @ApiOperation("检查用户是否在活动目标人群中")
    @GetMapping("/admin/marketing/marketingActivity/checkUserInTarget/{userId}/{activityId}")
    public Result<Boolean> checkUserInActivityTarget(@PathVariable Long userId, @PathVariable Long activityId) {
        return activityService.checkUserInActivityTarget(userId, activityId);
    }
}
